package com.zjdiepu.www.manage.view;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.CreditPeopleType;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.commpent.ViewScopeControllerHelper;
import com.zjdiepu.www.commpent.mapper.dz.DzApplyDiviVoMapper;
import com.zjdiepu.www.dao.yw.YwBusinessBuyerMapper;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.dz.IDzLoanMiddleBankService;
import com.zjdiepu.www.ibiz.icbc.IDzApplyDiviService;
import com.zjdiepu.www.model.dz.dto.DzLoanMiddleBank;
import com.zjdiepu.www.model.icbc.dto.DzApplyDivi;
import com.zjdiepu.www.model.yw.dto.YwBusinessBuyer;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * dz_申请分期Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年12月13日 18时05分<br/>
 * @since 1.0<br/>
 */
@Controller
@RequestMapping(value= "DzApplyDiviViewController", produces="text/html;charset=UTF-8")
@Api(tags = {"dz_申请分期"}, produces="text/html;charset=UTF-8")
public class DzApplyDiviViewController extends BaseController {
    @Resource
    private ViewScopeControllerHelper viewScopeControllerHelper;
    @Resource
    private IDzApplyDiviService dzApplyDiviService;
    @Resource
    private IDzLoanMiddleBankService dzLoanMiddleBankService;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;
    @Resource
    private DzApplyDiviVoMapper dzApplyDiviVoMapper;
    @Resource
    private YwBusinessBuyerMapper ywBusinessBuyerMapper;

    /**
     * @author xiongxiaotun <br/>
     * @Title: applyDiviDetail <br/>
     * @Description: dz_申请分期详情 <br/>
     * @param id
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "dz_申请分期详情", response = String.class, httpMethod="GET", tags={"申请分期详情"})
    @RequestMapping(value = "applyDiviDetail", method = RequestMethod.GET)
    public String applyDiviDetail(String id, Model model) {
    	if (StringUtils.isBlank(id)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	//下拉选项
    	model.addAttribute("hzicbcFeeMode", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.HZICBC_FEE_MODE,"请选择..."));
    	model.addAttribute("hzicbcTiexiFlag", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.HZICBC_TIEXI_FLAG,"请选择..."));
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(id, model);
    	if(ywBusinessDetail != null){
    		viewScopeControllerHelper.preparedHistoryBusiness(ywBusinessDetail.getCustomerNo(), model);
    		YwBusinessCar car = viewScopeControllerHelper.preparedCar(id, ywBusinessDetail.getCustomerId(),model);
    		if(car != null) {
    			viewScopeControllerHelper.preparedLoan(id, car.getId(), model, car.getCarType());
    		}
    		viewScopeControllerHelper.preparedRegistrationPlates(id, model);
    	}
    	DzApplyDivi dzApplyDivi = new DzApplyDivi();
    	dzApplyDivi.setDpStatus(DpStatus.NORMAL.getCode());
    	dzApplyDivi.setBusiId(id);
    	dzApplyDivi = dzApplyDiviService.selectRefined(dzApplyDivi);
    	if(null == dzApplyDivi) {
    		dzApplyDivi = new DzApplyDivi();
    	}
    	
    	DzLoanMiddleBank dzLoanMiddleBank = new DzLoanMiddleBank();
		dzLoanMiddleBank.setBusiId(id);
		dzLoanMiddleBank.setDpStatus(DpStatus.NORMAL.getCode());
		dzLoanMiddleBank = dzLoanMiddleBankService.selectRefined(dzLoanMiddleBank);
		if(null != dzLoanMiddleBank) {
			dzApplyDivi.setCard(dzLoanMiddleBank.getCardnoRefund());
		}
		
		YwBusinessBuyer ywBusinessBuyer = new YwBusinessBuyer();
		ywBusinessBuyer.setDpStatus(DpStatus.NORMAL.getCode());
		ywBusinessBuyer.setCarManType(CreditPeopleType.PEI_OU.getCode());
		ywBusinessBuyer.setBusiId(id);
		int wifeCount = ywBusinessBuyerMapper.countRefined(ywBusinessBuyer);
		model.addAttribute("hasPeiOu", wifeCount > 0);
    	
    	model.addAttribute("dzApplyDivi", dzApplyDivi);
    	viewScopeControllerHelper.preparedAttachmentEnum(model);
    	viewScopeControllerHelper.preparedNowTime(model);
    	return "daiZhongYw/DaiZhongXinXDengJi/autobankfangkuanDetail";
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: diviDetail <br/>
     * @Description: 申请分期详情 <br/>
     * @param id
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "申请分期详情", response = String.class, httpMethod="GET", tags={"申请分期详情"})
    @RequestMapping(value = "diviDetail", method = RequestMethod.GET)
    public String diviDetail(String id, Model model) {
    	if (StringUtils.isBlank(id)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	//下拉选项
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(id, model);
    	if(ywBusinessDetail != null){
    		viewScopeControllerHelper.preparedHistoryBusiness(ywBusinessDetail.getCustomerNo(), model);
    		YwBusinessCar car = viewScopeControllerHelper.preparedCar(id, ywBusinessDetail.getCustomerId(),model);
    		if(car != null) {
    			viewScopeControllerHelper.preparedLoan(id, car.getId(), model, car.getCarType());
    		}
    		viewScopeControllerHelper.preparedRegistrationPlates(id, model);
    	}
    	DzApplyDivi dzApplyDivi = new DzApplyDivi();
    	dzApplyDivi.setDpStatus(DpStatus.NORMAL.getCode());
    	dzApplyDivi.setBusiId(id);
    	dzApplyDivi = dzApplyDiviService.selectRefined(dzApplyDivi);
    	if(null == dzApplyDivi) {
    		dzApplyDivi = new DzApplyDivi();
    		DzLoanMiddleBank dzLoanMiddleBank = new DzLoanMiddleBank();
    		dzLoanMiddleBank.setBusiId(id);
    		dzLoanMiddleBank.setDpStatus(DpStatus.NORMAL.getCode());
    		dzLoanMiddleBank = dzLoanMiddleBankService.selectRefined(dzLoanMiddleBank);
    		if(null != dzLoanMiddleBank) {
    			dzApplyDivi.setCard(dzLoanMiddleBank.getCardnoRefund());
    		}
    	}
    	dzApplyDiviVoMapper.mapper(dzApplyDivi);
    	model.addAttribute("dzApplyDivi", dzApplyDivi);
    	
    	viewScopeControllerHelper.preparedNowTime(model);
    	return "daiZhongYw/DaiZhongXinXDengJi/autobankfangkuanQueryDetail";
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: suppleMaterialDetail <br/>
     * @Description: 补充分期材料 <br/>
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "补充分期材料 ", response = String.class, httpMethod="GET", tags={"补充分期材料 "})
    @RequestMapping(value = "suppleMaterialDetail", method = RequestMethod.GET)
    public String suppleMaterialDetail(String id, Model model) {
    	if (StringUtils.isBlank(id)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(id, model);
    	if(ywBusinessDetail != null){
    		viewScopeControllerHelper.preparedHistoryBusiness(ywBusinessDetail.getCustomerNo(), model);
    		YwBusinessCar car = viewScopeControllerHelper.preparedCar(id, ywBusinessDetail.getCustomerId(),model);
    		if(car != null) {
    			viewScopeControllerHelper.preparedLoan(id, car.getId(), model, car.getCarType());
    		}
    		viewScopeControllerHelper.preparedRegistrationPlates(id, model);
    	}
    	viewScopeControllerHelper.preparedAttachmentEnum(model);
    	return "system/yw/supplementaryMaterialDetail";
    }
}
